<?php

namespace Modules\Admin\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

class PermissionController extends Controller
{
    /**
     * 显示权限列表页面
     */
    public function index(): View
    {
        return view('admin::auth.permissions.index');
    }
    
    /**
     * 获取权限列表数据（用于Layui表格）
     */
    public function list(Request $request)
    {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $offset = ($page - 1) * $limit;
        
        $query = DB::table('admin_permissions')
            ->whereNull('deleted_at');
            
        // 搜索条件
        if ($request->filled('name')) {
            $query->where('name', 'like', '%' . $request->input('name') . '%');
        }
        
        if ($request->filled('slug')) {
            $query->where('slug', 'like', '%' . $request->input('slug') . '%');
        }
        
        // 总数
        $count = $query->count();
        
        // 获取分页数据
        $list = $query->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($limit)
            ->get();
            
        return response()->json([
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        ]);
    }
    
    /**
     * 显示创建权限页面
     */
    public function create(): View
    {
        return view('admin::auth.permissions.create');
    }
    
    /**
     * 保存新权限
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|max:50',
            'slug' => 'required|max:50|unique:admin_permissions,slug',
            'http_method' => 'nullable',
            'http_path' => 'nullable',
            'description' => 'nullable'
        ]);
        
        $data = $request->only(['name', 'slug', 'http_method', 'http_path', 'description']);
        $data['created_at'] = now();
        $data['updated_at'] = now();
        
        DB::table('admin_permissions')->insert($data);
        
        return response()->json([
            'code' => 0,
            'msg' => '添加成功'
        ]);
    }
    
    /**
     * 显示编辑权限页面
     */
    public function edit($id): View
    {
        $permission = DB::table('admin_permissions')->where('id', $id)->first();
        
        if (!$permission) {
            abort(404, '权限不存在');
        }
        
        return view('admin::auth.permissions.edit', compact('permission'));
    }
    
    /**
     * 更新权限
     */
    public function update(Request $request, $id)
    {
        $permission = DB::table('admin_permissions')->where('id', $id)->first();
        
        if (!$permission) {
            return response()->json([
                'code' => 1,
                'msg' => '权限不存在'
            ]);
        }
        
        $request->validate([
            'name' => 'required|max:50',
            'slug' => 'required|max:50|unique:admin_permissions,slug,' . $id,
            'http_method' => 'nullable',
            'http_path' => 'nullable',
            'description' => 'nullable'
        ]);
        
        $data = $request->only(['name', 'slug', 'http_method', 'http_path', 'description']);
        $data['updated_at'] = now();
        
        DB::table('admin_permissions')->where('id', $id)->update($data);
        
        return response()->json([
            'code' => 0,
            'msg' => '更新成功'
        ]);
    }
    
    /**
     * 删除权限
     */
    public function destroy($id)
    {
        $permission = DB::table('admin_permissions')->where('id', $id)->first();
        
        if (!$permission) {
            return response()->json([
                'code' => 1,
                'msg' => '权限不存在'
            ]);
        }
        
        // 软删除
        DB::table('admin_permissions')
            ->where('id', $id)
            ->update(['deleted_at' => now()]);
            
        return response()->json([
            'code' => 0,
            'msg' => '删除成功'
        ]);
    }
} 